package com.hooker.base;

import android.app.Application;
import android.content.Context;

import com.hooker.settings.Setting;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class BaseApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        if(!"com.tool.hooker".equals(getPackageName())) {
            Setting.hook(this, getPackageName());

            Setting.addPlugin(this, getApplicationInfo().sourceDir);

            settingPlugin();
        }
    }

    private void settingPlugin() {
        try {
            Class cls = Class.forName("com.hooker.plugins.PluginManager");
            if (cls != null) {
                Constructor constructor = cls.getDeclaredConstructor();
                if (constructor != null) {
                    Object pluginManager = constructor.newInstance();
                    Method init_method = cls.getDeclaredMethod("init", Context.class);
                    init_method.setAccessible(true);
                    init_method.invoke(pluginManager, this);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
